//
//  LXAuthorTableVC.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXAuthorTableVC.h"
#import "LXAuthorDetailCell.h"
@interface LXAuthorTableVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)NSMutableArray *allAlbum;
@property(nonatomic,copy)NSString *count;
@property(nonatomic,strong)UIImageView *headImage;
@property(nonatomic,strong)UIImageView *userImage;
@property(nonatomic,strong)UILabel *userNameLabel;
@property(nonatomic,strong)UILabel *userDetailLabel;
@property(nonatomic,strong)UILabel *otherLabel;
@property(nonatomic,strong)FXBlurView *blurView;
@end

@implementation LXAuthorTableVC
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = NO;
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.allArray = [NSMutableArray array];
    self.allAlbum = [NSMutableArray array];
    [self getData];
    [self createHeadImage];
    [self createTableView];
}
-(void)createHeadImage{
    self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, -200, SCREEN_WIDTH, 200)];
    self.blurView  = [[FXBlurView alloc]init];
    self.blurView.blurRadius = 40;
    [self.headImage addSubview:self.blurView];
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.headImage);
    }];
    
    self.userImage = [[UIImageView alloc]init];
    self.userImage.layer.cornerRadius = 40;
    self.userImage.layer.masksToBounds = YES;
    self.userImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.headImage addSubview:self.userImage];
    [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((SCREEN_WIDTH-80)/2);
//        make.right.mas_equalTo(-(SCREEN_WIDTH-80)/2);
        make.bottom.mas_equalTo(-90);
        make.height.mas_equalTo(80);
        make.width.mas_equalTo(80);
    }];
    self.userNameLabel = [[UILabel alloc]init];
    self.userNameLabel.font = [UIFont boldSystemFontOfSize:20];
    self.userNameLabel.textAlignment = NSTextAlignmentCenter;
//    self.userNameLabel.textColor = [UIColor colorWithWhite:0.835 alpha:1.000];
    [self.headImage addSubview:self.userNameLabel];
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userImage.mas_bottom).offset(5);
        make.width.mas_equalTo(300);
        make.left.mas_equalTo((SCREEN_WIDTH-300)/2);
        make.height.mas_equalTo(25);
    }];
    
    self.userDetailLabel = [[UILabel alloc]init];
    self.userDetailLabel.textAlignment = NSTextAlignmentCenter;
    self.userDetailLabel.font = [UIFont systemFontOfSize:15];
//    self.userDetailLabel.textColor = [UIColor colorWithWhite:0.835 alpha:1.000];
    [self.headImage addSubview:self.userDetailLabel];
    [self.userDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userNameLabel.mas_bottom).offset(5);
        make.left.width.equalTo(self.userNameLabel);
        make.height.mas_equalTo(20);
    }];
    
    self.otherLabel = [[UILabel alloc]init];
    self.otherLabel.textAlignment = NSTextAlignmentCenter;
    self.otherLabel.font = [UIFont systemFontOfSize:15];
//    self.otherLabel.textColor = [UIColor colorWithWhite:0.835 alpha:1.000];
    [self.headImage addSubview:self.otherLabel];
    [self.otherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userDetailLabel.mas_bottom).offset(5);
        make.left.height.width.equalTo(self.userDetailLabel);
    }];
}
-(void)createTableView{
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.myTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView addSubview:self.headImage];
    [self.myTableView registerClass:[LXAuthorDetailCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[LXAlbumCell class] forCellReuseIdentifier:@"albumcell"];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 30, 30)];
    [backBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
}
-(void)backBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    if (y < -180) {
        CGRect frame = self.headImage.frame;
        frame.origin.y = y;
        frame.size.height =  -y;
        self.headImage.frame = frame;
        if (240+y<0) {
            self.blurView.blurRadius = 0;
        }else{
        self.blurView.blurRadius = 240 + y;
        }
    }
}
-(void)getData{
//    发布的声音
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%ld/1/30?device=iPhone",[self.Uid integerValue]];
    [AFNetworkTool getUrl:str body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSArray *array = result[@"list"];
         self.count = [NSString stringWithFormat:@"%@",result[@"totalCount"]];
        for (NSDictionary *dic in array) {
            LXAuthorDetailModel *model = [LXAuthorDetailModel new];
            [model setValuesForKeysWithDictionary:dic];
            CGSize labelSize = [AppTools labelRectWithLabelSize:CGSizeMake(SCREEN_WIDTH-100-SPACE-SPACE-SPACE-SPACE-80, 100) LabelText:model.title Font:[UIFont systemFontOfSize:15]];
            model.labelHeight = labelSize.height;
            [self.allArray addObject:model];
        }
        [self.myTableView reloadData];
    } failure:^(NSError *error) {
    }];
//    头部
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?device=iPhone&toUid=%ld",[self.Uid integerValue]];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:result[@"backgroundLogo"]] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
        [self.userImage sd_setImageWithURL:[NSURL URLWithString:result[@"mobileSmallLogo"]] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
        self.userNameLabel.text = result[@"nickname"];
        self.userDetailLabel.text = result[@"personDescribe"];
        if ([result[@"followers"] integerValue]> 10000) {
            CGFloat follower = [result[@"followers"] integerValue]/10000.0;
            self.otherLabel.text = [NSString stringWithFormat:@"关注:%@    粉丝:%.1f万    赞过的:%@",result[@"followings"],follower,result[@"favorites"]];
        }else{
        self.otherLabel.text = [NSString stringWithFormat:@"关注:%@    粉丝:%@    赞过的:%@",result[@"followings"],result[@"followers"],result[@"favorites"]];
        }
    } failure:^(NSError *error) {
    }];
//    发布的专辑
    NSString *strURL = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/%ld/1/2?device=iPhone",[self.Uid integerValue]];
    [AFNetworkTool getUrl:strURL body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSArray *array = result[@"list"];
        for (NSDictionary *dic in array) {
            LXAlbumModel *model = [LXAlbumModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.allAlbum addObject:model];
        }
        [self.myTableView reloadData];

    } failure:^(NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _allAlbum.count;
    }else{
    return _allArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LXAlbumCell *album = [tableView dequeueReusableCellWithIdentifier:@"albumcell" forIndexPath:indexPath];
        album.cellModel = [_allAlbum objectAtIndex:indexPath.row];
        return album;
    }else{
    LXAuthorDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.cellModel = [_allArray objectAtIndex:indexPath.row];
    return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        KBAlbumsDetailViewController *detail = [KBAlbumsDetailViewController new];
        LXAlbumModel *album =  [_allAlbum objectAtIndex:indexPath.row];
        detail.albumId = [NSString stringWithFormat:@"%ld",album.albumId];
        [self.navigationController pushViewController:detail animated:YES];
    }else{
    LXAuthorDetailModel *model = [_allArray objectAtIndex:indexPath.row];
    LXMusicPlayerVC *music = [LXMusicPlayerVC new];
    music.Title = model.title;
    music.headImageLarge = model.coverLarge;
    music.songId = model.trackId;
    music.index = indexPath.row;
    music.allArray = [NSArray arrayWithArray:_allArray];
    music.totaltime = model.duration;
    music.subTitle = model.albumTitle;
        UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:music];
    [self presentViewController:navc animated:YES completion:nil];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100;
    }else{
    return SPACE+[_allArray[indexPath.row]labelHeight]+SPACE+SMALLSPICE+SMALLSPICE+20+20;
    }
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    myView.backgroundColor = [UIColor colorWithWhite:0.884 alpha:1.000];
    UILabel *myLabel = [[UILabel alloc]init];
    if (section == 0) {
        myLabel.text = [NSString stringWithFormat:@"发布的专辑"];
    }else{
    myLabel.text = [NSString stringWithFormat:@"发布的声音 (%@)",self.count];
    }
    myLabel.textColor = [UIColor lightGrayColor];
    [myView addSubview:myLabel];
    [myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(myView.mas_left).offset(20);
        make.top.mas_equalTo(myView.mas_top).offset(5);
        make.bottom.mas_equalTo(myView.mas_bottom).offset(-5);
        make.width.mas_equalTo(150);
    }];
    return myView;
}
@end
